namespace com.flajaxian
{
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using System;
    using System.Web;
    using Microsoft.Crm.Sdk.Utility;
    using System.Configuration;

    public class Attachment2DB : FileUploaderAdapter
    {
        private string crmserver;
        private static CrmService crmservice;
        private string domain;
        private string entityName;
        private string guid;
        private string orgname;
        private string password;
        private string username;

        public void Add(string data64string, string filename, string subject, string entityName, System.Guid guid)
        {
            annotation note = new annotation();
            note.filename = filename;
            if (crmservice == null)
            {
                //crmservice = this.GetCrmService();
                crmservice = CrmServiceUtility.GetCrmService(ConfigurationManager.AppSettings["CrmServer"], ConfigurationManager.AppSettings["OrgName"]); 
            }
            note.documentbody = data64string;
            note.subject = subject;
            note.objectid = new Lookup();
            note.objectid.type = entityName;
            note.objectid.Value = guid;
            note.objecttypecode = new EntityNameReference();
            note.objecttypecode.Value = entityName;
            crmservice.Create(note);
        }

        public void AddAttach(HttpPostedFile file)
        {
            byte[] buffer = new byte[file.ContentLength];
            file.InputStream.Read(buffer, 0, file.ContentLength);
            string encodedData = Convert.ToBase64String(buffer);
            this.Add(encodedData, file.FileName, null, this.entityName, new System.Guid(this.guid));
        }

        //public CrmService GetCrmService()
        //{
        //    IFDConnection ifdc = new IFDConnection(this.orgname, this.crmserver, this.domain, this.username, this.password);
        //    return ifdc.CrmService;
        //}

        public override void ProcessFile(HttpPostedFile file)
        {
            this.AddAttach(file);
        }

        public string Crmserver
        {
            get
            {
                return this.crmserver;
            }
            set
            {
                this.crmserver = value;
            }
        }

        public string Domain
        {
            get
            {
                return this.domain;
            }
            set
            {
                this.domain = value;
            }
        }

        public string EntityName
        {
            get
            {
                return this.entityName;
            }
            set
            {
                this.entityName = value;
            }
        }

        public string Guid
        {
            get
            {
                return this.guid;
            }
            set
            {
                this.guid = value;
            }
        }

        public string Orgname
        {
            get
            {
                return this.orgname;
            }
            set
            {
                this.orgname = value;
            }
        }

        public string Password
        {
            get
            {
                return this.password;
            }
            set
            {
                this.password = value;
            }
        }

        public string Username
        {
            get
            {
                return this.username;
            }
            set
            {
                this.username = value;
            }
        }
    }
}
